package com.example.tests;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;




public class TestBase {
	
	protected static ApplicationManager app;
	
	@BeforeTest
	public void setUp() throws Exception {
		app = new ApplicationManager();
	}

	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator(){
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++) {
			GroupData group = new GroupData()
			.withName(generateRandomString())
			.withHeader(generateRandomString())
			.withFooter(generateRandomString());
			list.add(new Object[]{group});
		}
		return list.iterator();			
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator() throws ParseException{
		List<Object[]> list = new ArrayList<Object[]>();
		
		for (int i = 0; i < 5; i++) {
			String date = generateRandomDate();
			ContactData contact = new ContactData()	
			.withFirstName(generateRandomString())
			.withLastName(generateRandomString())
			.withAddress(generateRandomString())
			.withHomePhoneNumber(generateRandomString())
			.withMobilePhoneNumber(generateRandomString())
			.withWorkPhoneNumber(generateRandomString())
			.withFirstEmail(generateRandomString())
			.withSecondEmail(generateRandomString())
			.withBmonth(date.substring(4, date.lastIndexOf("-")))
			.withBday(date.substring(date.lastIndexOf("-") + 1, date.length()))
			.withbirthYear(date.substring(0, 4))
			.withAddress2(generateRandomString())
			.withHomePhone2(generateRandomString());
		//	contact.newGroup = "group name 1";
			list.add(new Object[]{contact});
		}
		return list.iterator();			
	}
	
	public String generateRandomString(){
		Random rnd = new Random();
		if ( rnd.nextInt(3) == 0){
			return "";
		} else {
		return "test" + rnd.nextInt();}
	}

	public String generateRandomYear(){
		Random rnd = new Random();
		int year = rnd.nextInt(50) + 1950;
        return "" + year;
	}
	
	public String generateRandomDate() throws ParseException{
		Random rnd = new Random();
		int year = rnd.nextInt(50) + 1950;
		int month = rnd.nextInt(11) + 1;
		SimpleDateFormat monthParse = new SimpleDateFormat("MM");
		SimpleDateFormat monthDisplay = new SimpleDateFormat("MMMM",Locale.US);
		String monthInLetters = monthDisplay.format(monthParse.parse("" + month));
		GregorianCalendar gc = new GregorianCalendar(year, month, 1);    
        int day = rnd.nextInt(gc.getActualMaximum(Calendar.DAY_OF_MONTH) + 1);
  
        return (year + monthInLetters + "-" + day);
	}
}
